//---------------------------------------------------------------------------------
// Copyright (c) 2008 VoiceLynx Project
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//     1. The origin of this software must not be misrepresented; you must not
//        claim that you wrote the original software. If you use this software
//        in a product, an acknowledgment in the product documentation would be
//        appreciated but is not required.
//
//     2. Altered source versions must be plainly marked as such, and must not be
//        misrepresented as being the original software.
//
//     3. This notice may not be removed or altered from any source
//        distribution.
//---------------------------------------------------------------------------------

#ifndef __VLClientApp_h__
#define __VLClientApp_h__

#include "VLCore.h"
#include "VLStandAlone.h"
#include "UI_MainFrame.h"

#include "VLHidManager.h"
#include "VLClientControllers.h"

#define VL_BEGIN_CORE_CRITICAL_SECTION()                          \
      {                                                           \
        wxCriticalSectionLocker __critical_section_locker(wxGetApp().getCriticalSection());

#define VL_END_CORE_CRITICAL_SECTION()                            \
      }

class VLClientApp;

//! Seperate thread to hit the loop() function on VLClientApp

/// This thread runs to constantly to hit the loop() function of the app, which
/// in turn updates core, input, network, etc.
class VLCoreLoopThread : wxThread
{
public:
  //! Create the main thread, pass in the core that owns it
  VLCoreLoopThread(VLClientApp &core);

  //! Start the thread
  void start();

  //! Request to stop the thread (should happen very quickly)
  void stop();

  //! The thread's entry point - infinite loops and calls loop() on core
  void *Entry();

private:
  VLClientApp &mApp;
};

// Define a new application type, each program should derive a class from wxApp
class VLClientApp : public wxApp
{
public:
  VLClientApp();

  // override base class virtuals
  // ----------------------------

  // this one is called on application startup and is a good place for the app
  // initialization (doing it here and not in the ctor allows to have an error
  // return: if OnInit() returns false, the application terminates)
  virtual bool OnInit();

  virtual int OnExit();

  UI_MainFrameExt *getMainFrame();

  void loop();

  wxCriticalSection &getCriticalSection()
  {
    return mCoreCriticalSection;
  }

  VLClientCore &getCore()
  {
    return mClient.getCore();
  }

  VLStandaloneClient &getClient()
  {
    return mClient;
  }

  const VLUserIdToUserMap &getUsers()
  {
    return mClient.getUsers();
  }

private:
  wxCriticalSection             mCoreCriticalSection;

  //std::auto_ptr<VLClientCore>   mCore;           //auto_ptr to delay construction until OnInit()
  VLStandaloneClient            mClient;
  VLCoreLoopThread              mCoreThread;

  VLHidManager                  mHidManager;
  VLPushToTalkController        mPushToTalkController;

  UI_MainFrameExt              *mMainFrame;
};

DECLARE_APP(VLClientApp);

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// IDs for the controls and the menu commands
enum
{
  // menu items
  Minimal_Quit = wxID_EXIT,

  // it is important for the id corresponding to the "About" command to have
  // this standard value as otherwise it won't be handled properly under Mac
  // (where it is special and put into the "Apple" menu)
  Minimal_About = wxID_ABOUT
};



#endif